Skip to main content

Class SamplingSensorBase<UNIT>

Base class for sensors and other updating classes that want to support having their updates consumed by observers that can optionally use filters

Assembly: Meadow.Foundation.dll
View Source
Declaration
public abstract class SamplingSensorBase<UNIT> : ObservableBase<UNIT>, IObservable<IChangeResult<UNIT>>, ISamplingSensor<UNIT>, ISensor<UNIT>, ISensor, ISamplingSensor where UNIT : struct

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Derived:

Expand

Implements:
System.IObservable<Meadow.IChangeResult<<UNIT>>>, Meadow.Peripherals.Sensors.ISamplingSensor<<UNIT>>, Meadow.Peripherals.Sensors.ISensor<<UNIT>>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor

Properties

SamplingTokenSource

Sampling cancellation token source

View Source
Declaration
protected CancellationTokenSource? SamplingTokenSource { get; set; }

Conditions

The last read conditions

View Source
Declaration
public UNIT Conditions { get; protected set; }

IsSampling

Gets a value indicating whether the sensor is currently in a sampling loop. Call StartSampling() to spin up the sampling process.

View Source
Declaration
public bool IsSampling { get; protected set; }

UpdateInterval

A TimeSpan that specifies how long to wait between readings. This value influences how often *Updated events are raised and IObservable consumers are notified.

View Source
Declaration
public virtual TimeSpan UpdateInterval { get; protected set; }

Fields

samplingLock

Lock for sampling

View Source
Declaration
protected object samplingLock

Methods

RaiseUpdated(UNIT)

Raised when the sensor has new data

View Source
Declaration
protected void RaiseUpdated(UNIT newValue)
Parameters
TypeNameDescription
<UNIT>newValueThe new sensor value as a generic

ReadSensor()

Read value from sensor

View Source
Declaration
protected abstract Task<UNIT> ReadSensor()
Returns

System.Threading.Tasks.Task<<UNIT>>: unitized value### RaiseEventsAndNotify(IChangeResult<UNIT>) Notify observers

View Source
Declaration
protected virtual void RaiseEventsAndNotify(IChangeResult<UNIT> changeResult)
Parameters
TypeNameDescription
Meadow.IChangeResult<<UNIT>>changeResultprovides new and old values

Read()

Convenience method to get the current sensor readings. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

View Source
Declaration
public virtual Task<UNIT> Read()
Returns

System.Threading.Tasks.Task<<UNIT>>

StartUpdating(TimeSpan?)

Starts updating the sensor on the updateInterval frequency specified

View Source
Declaration
public abstract void StartUpdating(TimeSpan? updateInterval = null)
Parameters
TypeNameDescription
System.Nullable<System.TimeSpan>updateIntervalA TimeSpan that specifies how long to
wait between readings

StopUpdating()

Stops sampling the sensor

View Source
Declaration
public abstract void StopUpdating()

Events

Updated

Event handler for updated values

View Source
Declaration
public event EventHandler<IChangeResult<UNIT>> Updated
Event Type

System.EventHandler<Meadow.IChangeResult<<UNIT>>>

Implements

  • System.IObservable<Meadow.IChangeResult<<UNIT>>>
  • Meadow.Peripherals.Sensors.ISamplingSensor<<UNIT>>
  • Meadow.Peripherals.Sensors.ISensor<<UNIT>>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor